﻿using System;
using System.Collections;
using System.Reflection;
using ESRI.ArcGIS.ADF.BaseClasses;
using DevComponents.DotNetBar;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.SystemUI;
using System.Drawing;
using System.Windows.Forms;
using ICommand = ESRI.ArcGIS.SystemUI.ICommand;
using System.Runtime.InteropServices;
using PlanningGIS.Util.Log;

namespace PlanningGIS.Framework
{
    /// <summary>
    /// 按钮对象池扩展类
    /// </summary>
    public class CommandPoolExClass : ICommandPoolEx
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CommandPoolExClass()
        {
            _HashtableCommand = new Hashtable();
            _HashtableBaseItem = new Hashtable();
        }

        /// <summary>
        /// 执行按钮集合
        /// </summary>
        private Hashtable _HashtableCommand = null;

        /// <summary>
        /// 按钮集合
        /// </summary>
        private Hashtable _HashtableBaseItem = null;

        /// <summary>
        /// 地图关联对象
        /// </summary>
        private object m_Hook;
        
        /// <summary>
        /// 添加安装
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public BaseItem AddCommand(object obj)
        {
            if (FindCommand(obj.ToString()) == null)
                this._HashtableCommand.Add(obj.ToString(), obj);
            if (obj is UserControl || obj is Control)
            {
                return GetUserControl(obj);
            }
            else if (obj is IToolControl)
            {
                return GetToolControl(obj);
            }
            else if (obj is ICommand)
            {
                return GetCommand(obj);
            }
            return null;
        }

        /// <summary>
        /// 获取用户控件对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private BaseItem GetUserControl(object obj)
        {
            ControlContainerItem pItem = new ControlContainerItem();
            try
            {
                pItem.AllowItemResize = false;
                if (obj is UserControl)
                {
                    UserControl myControl = obj as UserControl;
                    myControl.Dock = DockStyle.Fill;
                    myControl.BackColor = Color.Transparent;
                    pItem.Control = myControl;
                }
                else
                {
                    Control myControl = obj as Control;
                    myControl.Dock = DockStyle.Fill;
                    myControl.BackColor = Color.Transparent;
                    pItem.Control = myControl;
                }
                pItem.AllowItemResize = false;
                pItem.Tooltip = (obj as ICommand).Tooltip;
                
                pItem.Style = eDotNetBarStyle.StyleManagerControlled;
                pItem.MenuVisibility = DevComponents.DotNetBar.eMenuVisibility.VisibleAlways;
                pItem.Name = obj.ToString();
                if (_HashtableBaseItem.ContainsKey(obj.ToString()) == false)
                    this._HashtableBaseItem.Add(obj.ToString(), pItem);
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorText(ex);
            }            
            return pItem;
        }

        /// <summary>
        /// 获取按钮命令对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private BaseItem GetCommand(object obj)
        {
            ButtonItem m_ButtonItem = new ButtonItem();            
            try
            {
                ICommand pCommand = (ICommand)obj;
                m_ButtonItem.Name = obj.ToString();
                m_ButtonItem.Text = pCommand.Caption;
                m_ButtonItem.ButtonStyle = eButtonStyle.ImageAndText;
                m_ButtonItem.ImagePosition = eImagePosition.Top;
                m_ButtonItem.ImageFixedSize = new Size(32, 32);
                if (pCommand.Bitmap != 0)
                {
                    Bitmap m_bitmap = System.Drawing.Bitmap.FromHbitmap((IntPtr)pCommand.Bitmap);
                    m_bitmap.MakeTransparent();
                    m_ButtonItem.Image = m_bitmap;
                }
                m_ButtonItem.Tooltip = pCommand.Tooltip;
                m_ButtonItem.Tag = obj;
                if (_HashtableBaseItem.ContainsKey(obj.ToString()) == false)
                    this._HashtableBaseItem.Add(obj.ToString(), m_ButtonItem);               
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorText(ex);
            }
            return m_ButtonItem;           
        }

        /// <summary>
        /// 获取工具按钮对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private BaseItem GetToolControl(object obj)
        {
            ControlContainerItem pItem = new ControlContainerItem();
            try
            {
                Control ctl = Control.FromHandle(new IntPtr((obj as IToolControl).hWnd));
                pItem.AllowItemResize = false;
                ctl.Dock = DockStyle.Fill;
                pItem.Control = ctl;
                pItem.AllowItemResize = false;
                pItem.MenuVisibility = DevComponents.DotNetBar.eMenuVisibility.VisibleAlways;
                pItem.Name = obj.ToString();
                if (_HashtableBaseItem.ContainsKey(obj.ToString()) == false)
                    this._HashtableBaseItem.Add(obj.ToString(), pItem);
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorText(ex);
            }           
            return pItem;
        }
        
        /// <summary>
        /// 查找按钮
        /// </summary>
        /// <param name="commandName"></param>
        /// <returns></returns>
        public BaseItem FindBaseItem(string commandName)
        {
            if (_HashtableBaseItem[commandName] != null)
                return _HashtableBaseItem[commandName] as BaseItem;
            else
                return null;
        }

        /// <summary>
        /// 查找按钮命令对象
        /// </summary>
        /// <param name="commandName"></param>
        /// <returns></returns>
        public ICommand FindCommand(string commandName)
        {
            if (_HashtableCommand[commandName] != null)
                return _HashtableCommand[commandName] as ICommand;
            else
                return null;
        }

        /// <summary>
        /// 获取按钮命令对象
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public ICommand get_Command(int index)
        {
            if (index < this._HashtableCommand.Count && index > -1)
                return (_HashtableCommand[index] as ButtonItem).Tag as ICommand;
            else
                return null;
        }

        /// <summary>
        /// 加载按钮命令对象
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="className"></param>
        /// <returns></returns>
        public ICommand LoadCommand(string fileName, string className)
        {
            try
            {
                Assembly assembly = Assembly.Load(fileName);
                object provider = assembly.CreateInstance(className);
                if (provider is ICommand)
                {
                    return provider as ICommand;
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorText(ex);
            }           
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="className"></param>
        /// <param name="subType"></param>
        /// <returns></returns>
        public ICommand LoadCommand(string fileName, string className, int subType)
        {
            try
            {
                Assembly assembly = Assembly.Load(fileName);
                object provider = assembly.CreateInstance(className);
                if (provider is ICommandSubType)
                {
                    ICommandSubType pCommandSubType = provider as ICommandSubType;
                    pCommandSubType.SetSubType(subType);
                    return provider as ICommand;
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorText(ex);
            }
            return null;
        }

        /// <summary>
        /// 更新按钮命令状态
        /// </summary>
        public void UpdateCommandState()
        {
            try
            {
                BaseItem m_BaseItem = null;
                ICommand m_Command = null;
                IDictionaryEnumerator pEnumerator = _HashtableBaseItem.GetEnumerator();
                pEnumerator.Reset();
                while (pEnumerator.MoveNext())
                {
                    m_BaseItem = pEnumerator.Value as BaseItem;
                    m_Command = this.FindCommand(m_BaseItem.Name);
                    try
                    {
                        if (m_BaseItem.Enabled != m_Command.Enabled)
                            m_BaseItem.Enabled = m_Command.Enabled;

                        if (m_BaseItem is ButtonItem)
                        {
                            if ((m_BaseItem as ButtonItem).Checked != m_Command.Checked)
                                (m_BaseItem as ButtonItem).Checked = m_Command.Checked;
                            if ((m_Hook as IFramework).Tool == m_Command)
                                (m_BaseItem as ButtonItem).Checked = true;

                        }
                        if (m_BaseItem is ControlContainerItem && (m_BaseItem as ControlContainerItem).Control.Enabled != m_Command.Enabled)
                        {
                            (m_BaseItem as ControlContainerItem).Control.Enabled = m_Command.Enabled;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.WriteErrorText(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorText(ex);
            }
        }

        /// <summary>
        /// 异常按钮命令
        /// </summary>
        /// <param name="pCommand"></param>
        public void Remove(ICommand pCommand)
        {
            this._HashtableCommand.Remove(pCommand.Name);
        }

        /// <summary>
        /// 移除所有的命令
        /// </summary>
        public void RemoveAll()
        {
            this._HashtableCommand.Clear();
        }

        /// <summary>
        /// 设置地图关联对象
        /// </summary>
        /// <param name="pHook"></param>
        public void SetHook(object pHook)
        {
            m_Hook = pHook;
        }

        /// <summary>
        /// 获取按钮的总数
        /// </summary>
        public int Count
        {
            get
            {
                return _HashtableCommand.Count;
            }
        }

        /// <summary>
        /// 获取地图关联对象
        /// </summary>
        public object Hook
        {
            get
            {
                return m_Hook;
            }
            set
            {
                this.m_Hook = value;
            }
        }
    }
}

